

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>dscribe.utils.average_kernel &mdash; DScribe 0.2.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/style.css" type="text/css" />
    <link rel="author" title="About these documents" href="../../../about.html" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                0.2.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../install.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorials/tutorials.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../doc/modules.html">Documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../citing.html">Citing DScribe</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../about.html">About</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">DScribe</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>dscribe.utils.average_kernel</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for dscribe.utils.average_kernel</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">dscribe</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">ase</span>
<span class="kn">from</span> <span class="nn">scipy.spatial.distance</span> <span class="k">import</span> <span class="n">cdist</span>

<div class="viewcode-block" id="AverageKernel"><a class="viewcode-back" href="../../../doc/dscribe.utils.html#dscribe.utils.average_kernel.AverageKernel">[docs]</a><span class="k">class</span> <span class="nc">AverageKernel</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Rematch Kernel methods to go from local descriptors</span>
<span class="sd">    to global similarity</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span>

<div class="viewcode-block" id="AverageKernel.compute_gaussian"><a class="viewcode-back" href="../../../doc/dscribe.utils.html#dscribe.utils.average_kernel.AverageKernel.compute_gaussian">[docs]</a>    <span class="k">def</span> <span class="nf">compute_gaussian</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ave_a</span><span class="p">,</span><span class="n">ave_b</span><span class="p">,</span> <span class="n">gamma</span> <span class="o">=</span> <span class="mf">0.01</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Takes two matrices and computes the similarity</span>
<span class="sd">        based on the gaussian kernel.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">gamma</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(((</span><span class="n">ave_a</span> <span class="o">-</span> <span class="n">ave_b</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()))</span></div>

<div class="viewcode-block" id="AverageKernel.distance_matrix_to_gaussian_kernel"><a class="viewcode-back" href="../../../doc/dscribe.utils.html#dscribe.utils.average_kernel.AverageKernel.distance_matrix_to_gaussian_kernel">[docs]</a>    <span class="k">def</span> <span class="nf">distance_matrix_to_gaussian_kernel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dist_matrix</span><span class="p">,</span> <span class="n">gamma</span> <span class="o">=</span> <span class="mf">0.01</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Takes a distance matrix and computes the similarity</span>
<span class="sd">        based on the gaussian kernel.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">gamma</span> <span class="o">*</span> <span class="n">dist_matrix</span><span class="p">)</span></div>

<div class="viewcode-block" id="AverageKernel.get_global_distance_matrix"><a class="viewcode-back" href="../../../doc/dscribe.utils.html#dscribe.utils.average_kernel.AverageKernel.get_global_distance_matrix">[docs]</a>    <span class="k">def</span> <span class="nf">get_global_distance_matrix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">desc_list</span><span class="p">,</span> <span class="n">metric</span> <span class="o">=</span> <span class="s1">&#39;euclidean&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Takes a list of M x N matrices, where M is the number of atoms</span>
<span class="sd">        in the system and N is the number of features of the descriptor.</span>
<span class="sd">        The matrices can be of different sizes.</span>
<span class="sd">        Returns a squared matrix with the size of the given dataset.</span>
<span class="sd">        Args:</span>
<span class="sd">            desc_list: list of NAxM matrices of structure A with NA atoms and M features.</span>
<span class="sd">            metric: Either string of valid cdist metric or custom function</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1">#N = int(len(desc_list))</span>
        <span class="n">ave_desc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">average_descriptor</span><span class="p">(</span><span class="n">desc_list</span><span class="p">)</span>

        <span class="n">dist_matrix</span> <span class="o">=</span> <span class="n">cdist</span><span class="p">(</span><span class="n">ave_desc</span><span class="p">,</span> <span class="n">ave_desc</span><span class="p">,</span> <span class="n">metric</span> <span class="o">=</span> <span class="n">metric</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">dist_matrix</span></div>


<div class="viewcode-block" id="AverageKernel.average_descriptor"><a class="viewcode-back" href="../../../doc/dscribe.utils.html#dscribe.utils.average_kernel.AverageKernel.average_descriptor">[docs]</a>    <span class="k">def</span> <span class="nf">average_descriptor</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">desc_list</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the average global similarity between two structures A and B,</span>
<span class="sd">        as reported in:</span>
<span class="sd">        Phys. Chem. Chem. Phys., 2016, 18, p. 13768</span>
<span class="sd">        Takes a list of NA x M matrices, where NA is the number of atoms</span>
<span class="sd">        in the system and M is the number of features of the descriptor.</span>
<span class="sd">        The matrices can be of different sizes.</span>
<span class="sd">        Returns a squared matrix with the size of the given dataset.</span>
<span class="sd">        Args:</span>
<span class="sd">            desc_list: list of NAxM matrices of structure A with NA atoms and M features.</span>
<span class="sd">        Returns:</span>
<span class="sd">            average_descriptor matrix: NxM matrix with N datapoints and M features</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">N</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">desc_list</span><span class="p">)</span>
        <span class="n">M</span> <span class="o">=</span> <span class="n">desc_list</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

        <span class="n">ave_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">N</span><span class="p">,</span> <span class="n">M</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">structure_matrix</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">desc_list</span><span class="p">):</span>
            <span class="n">ave_matrix</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">structure_matrix</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">axis</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">ave_matrix</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>